/* 
 * Copyright (C) 2008 Torgny Bjers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sugree.jibjib;

import java.util.Locale;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
import android.text.util.Linkify;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.sugree.jibjib.data.JibJibDatabase;
import com.sugree.jibjib.data.JibJibDatabase.DirectMessages;
import com.sugree.jibjib.data.Tweet;
import com.sugree.jibjib.util.RelativeTime;

/**
 * @author torgny.bjers
 *
 */
public class MessageActivity extends TweetActivity {

	protected final String[] PROJECTION = new String[] {
		DirectMessages._ID,
		DirectMessages.AUTHOR_ID,
		DirectMessages.MESSAGE,
		DirectMessages.SENT_DATE
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mFavorite.setVisibility(View.GONE);
	}

	@Override
	protected String[] getProjection() {
		return PROJECTION;
	}

	@Override
	protected void handleResume() {
		if (mCursor != null) {
			mCursor.moveToFirst();
			String aAuthor = mCursor.getString(mCursor.getColumnIndex(DirectMessages.AUTHOR_ID));
			String aMessage = mCursor.getString(mCursor.getColumnIndex(DirectMessages.MESSAGE));
			long aSentDate = mCursor.getLong(mCursor.getColumnIndex(DirectMessages.SENT_DATE));
			mAuthor.setText(aAuthor);
			mMessage.setLinksClickable(true);
			mMessage.setFocusable(true);
			mMessage.setFocusableInTouchMode(true);
			mMessage.setText(aMessage);
			Linkify.addLinks(mMessage, Linkify.ALL);

			Pattern geoMatcher = Pattern.compile(GEO_PATTERN);
			Linkify.addLinks(mMessage, geoMatcher, GEO_SCHEME, new GeoMatchFilter(), new GeoTransform());

			Pattern meMatcher = Pattern.compile(ME_PATTERN);
			Linkify.addLinks(mMessage, meMatcher, "");

			String sentDate = RelativeTime.getDifference(this, aSentDate);
			mSentDate.setText(sentDate);

			extractUrl(aMessage);

			mTweet = new Tweet(
					mSP.getString("twitter_username", null),
					aAuthor,
					aMessage);
		} else {
			Log.e(TAG, "No cursor found");
		}
	}
}
